package Model;

import java.sql.ResultSet;
import java.sql.Statement;

public class User {

	   private DbConnection db = null;
	   private Statement st = null;
	   private ResultSet rs = null; 
	   
	   private int id_user=-1;
	   private String name=null;
	   private String email=null;
	   private String login=null;
	   private String pass=null;
	   private int role=-1;
	   
	   
	   public User(){
		   db = new DbConnection();	   
		   st = db.getStatement();
	   }
	   
	   public User(String login){
		   db = new DbConnection();	   
		   st = db.getStatement();
		   
		   try{
				this.rs = this.st.executeQuery("Select * from \"user\" "+
												" WHERE login='"+login+"';");
				this.rs.next();
				this.id_user = this.rs.getInt("id_user");
				this.name = this.rs.getString("name");
				this.email= this.rs.getString("email");
				this.login = this.rs.getString("login");
				this.pass = this.rs.getString("pass");
				this.role = this.rs.getInt("role");
				
				
			}catch (Exception e) {
				System.out.print(e.toString());
			}   
	   }
	   
	   public int getIdUser(){
		   return this.id_user;
	   }
	   
	   public String getName(){
		   return this.name;
	   }
	   
	   public String getEmail(){
		   return this.email;
	   }
	   
	   public String getLogin(){
		   return this.login;
	   }
	   
	   public String getPass(){
		   return this.pass;
	   }
	   
	   public int getRole(){
		   return this.role;
	   }
	   
	   public void setUser(String login){
		   try{
				this.rs = this.st.executeQuery("Select * from \"user\" "+
												" WHERE login='"+login+"';");
				this.rs.next();
				this.id_user = this.rs.getInt("id_user");
				this.name = this.rs.getString("name");
				this.email= this.rs.getString("email");
				this.login = this.rs.getString("login");
				this.pass = this.rs.getString("pass");
				this.role = this.rs.getInt("role");
				
				
			}catch (Exception e) {
				System.out.print(e.toString());
			}   
	   }
	   
	   
	   //METODY BAZODANOWE:

	   public int insertUser(String name, String email, String login, String pass, int role){
		   int result=-1;
		   try{
				this.rs = this.st.executeQuery("INSERT INTO \"user\"( "+
			            " \"name\", email, \"login\", pass, \"role\") "+
			            " VALUES ( '"+name+"', '"+email+"', '"+login+"', '"+pass+"', "+role+") returning id_user;");
				
				this.rs.next();
				result = this.rs.getInt("id_user");
				
			}catch (Exception e) {
				System.out.print(e.toString());
				return result;
			}
			return result;
		}
	   
	   public int deleteUserById(int id){
		   
		   int result = -1;
			try{
				this.rs = this.st.executeQuery("DELETE FROM \"user\" WHERE id_user="+id+" returning id_user");
				
				this.rs.next();
				result = this.rs.getInt("id_user");
				
			}catch (Exception e) {
				System.out.print(e.toString());
				return result;
			}
			return result;
		   
	   }
	   
	   public int updateUser(int id_user,String name, String email, String login, String pass, int role){
		   int result=-1;
		   try{
				this.rs = this.st.executeQuery("UPDATE \"user\" "+
											   "SET \"name\"='"+name+"', email='"+email+"', \"login\"='"+login+"', pass='"+pass+"', \"role\"="+role+
											  " WHERE id_user="+id_user+" returning id_user;");
				
				this.rs.next();
				result = this.rs.getInt("id_user");
				
			}catch (Exception e) {
				System.out.print(e.toString());
				return result;
			}
			return result;
	   }
	   
	   
	
}
